﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using Hsinchu.Model;
using Hsinchu.Utility;


namespace Hsinchu.BLL
{
    /*
     * 产品业务逻辑
     */
    public class ItemMgr
    {
        DAL.DataProcess dllDataProcess = new Hsinchu.DAL.DataProcess();


        /// <summary>
        /// 添加新产品
        /// </summary>
        /// <param name="item">产品对象</param>
        public void AddItem(Model.MItem item)
        {
            string _spName = "Insert_Item";
            object[] _value = new object[3];
            _value[0] = item.Name;
            _value[1] = item.CategoryId;
            _value[2] = item.TableName;
            dllDataProcess.getAffectRows(_spName, _value);
        }

        /// <summary>
        /// 根据产品状态选择产品
        /// </summary>
        /// <returns></returns>
        public DataTable GetItemByIsDisable(int isdisable)
        {
            string _spName = "Select_ItemByIsDisable";
            object[] _value = new object[1];
            _value[0] = isdisable;
            return dllDataProcess.getDataTable(_spName, _value);
        }




        /// <summary>
        /// 根据产品id状态为停售
        /// </summary>
        /// <param name="itemid"></param>
        public void DisableItem(List<int> itemid)
        {
            string _spName = "Update_ItemByDisable";
            List<object[]> _value = new List<object[]>();
            foreach (int id in itemid)
            {
                object[] obj = new object[1];
                obj[0] = id;
                _value.Add(obj);
            }
            dllDataProcess.getAffectRowsWithTransaction(_spName, _value);
        }

        /// <summary>
        /// 根据产品id状态为出售
        /// </summary>
        /// <param name="itemid"></param>
        public void UnDisableItem(List<int> itemid)
        {
            string _spName = "Update_ItemByUnDisable";
            List<object[]> _value = new List<object[]>();
            foreach (int id in itemid)
            {
                object[] obj = new object[1];
                obj[0] = id;
                _value.Add(obj);
            }
            dllDataProcess.getAffectRowsWithTransaction(_spName, _value);
        }

        /// <summary>
        /// 根据产品id删除
        /// </summary>
        /// <param name="itemid"></param>
        public void DeleteItem(List<int> itemid)
        {
            string _spName = "Delete_ItemByItemId";
            List<object[]> _value = new List<object[]>();
            foreach (int id in itemid)
            {
                object[] obj = new object[1];
                obj[0] = id;
                _value.Add(obj);
            }
            dllDataProcess.getAffectRowsWithTransaction(_spName, _value);
        }


        /// <summary>
        /// 读取属性xml
        /// </summary>
        /// <param name="tablename"></param>
        /// <returns></returns>
        public DataTable GetProperty(string docpath,string tablename)
        {
            DataTable dtSchema = GetPropertySchema();
            dtSchema=XMLHelper.GetXmlToDataTable(docpath, tablename, dtSchema);
            return dtSchema;
   
        }

        /// <summary>
        /// 生成属性DataTable
        /// </summary>
        /// <returns>属性datatable</returns>
        DataTable GetPropertySchema()
        {
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("PropertyEName");
            DataColumn dc2 = new DataColumn("PropertyCName");
            DataColumn dc3 = new DataColumn("PropertyType");
            DataColumn dc4 = new DataColumn("PropertySize");
            DataColumn dc5 = new DataColumn("PropertyDisable");
            DataColumn dc6 = new DataColumn("PropertyDisplay");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);
            dt.Columns.Add(dc6);
            return dt;
        }

        /// <summary>
        /// 添加属性
        /// </summary>
        /// <param name="sql"></param>
        public void AddProperty(string tablename, string fieldname, string fieldtype, string fieldsize)
        {
            string sql = dllDataProcess.GenerationSql(tablename, fieldname, fieldtype, fieldsize);
            dllDataProcess.getAffectRowsBySql(sql);
        }
    }
}
